package com.whfc.iot.util;

import com.whfc.common.util.IPUtil;
import io.netty.channel.Channel;

import java.net.InetSocketAddress;

/**
 * @Description:
 * @author: xugcheng
 * @version: 1.0
 * @date: 2019/8/21 17:59
 */
public class ChannelUtil {

    /**
     * 阿里云负载均衡-健康检查的IP地址段
     */
    public static final String ALIYUN_SLB_HEALTH_CHECK_CIDR = "100.64.0.0/10";

    /**
     * 获取客户端IP
     *
     * @param channel
     * @return
     */
    public static String getRemoteIp(Channel channel) {
        InetSocketAddress remoteAddress = (InetSocketAddress) channel.remoteAddress();
        String remoteIp = remoteAddress.getAddress().getHostAddress();
        return remoteIp;
    }

    /**
     * 判断channel是否slb健康检查
     *
     * @param channel
     * @return
     */
    public static boolean isSLBCheck(Channel channel) {
        String clientIP = getRemoteIp(channel);
        return IPUtil.checkCIDR(clientIP, ALIYUN_SLB_HEALTH_CHECK_CIDR);
    }
}
